package com.miaostars.apakey.events;

import com.miaostars.apakey.Apakey;
import com.miaostars.apakey.checks.InventoryChecks;
import com.miaostars.apakey.utils.AlertManager;
import com.miaostars.apakey.utils.PlayerData;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;

public class InventoryListener implements Listener {
    
    private final Apakey plugin;
    private final InventoryChecks inventoryChecks;
    private final AlertManager alertManager;
    
    public InventoryListener(Apakey plugin) {
        this.plugin = plugin;
        this.inventoryChecks = new InventoryChecks(plugin);
        this.alertManager = plugin.getAlertManager();
    }
    
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (!(event.getWhoClicked() instanceof Player)) {
            return;
        }
        
        Player player = (Player) event.getWhoClicked();
        PlayerData data = PlayerData.get(player);
        
        // Check for item duplication
        ItemStack clicked = event.getCurrentItem();
        if (inventoryChecks.checkItemDupe(player, data, clicked)) {
            alertManager.alert(player, "ItemDupe", "A");
            event.setCancelled(true);
        }
    }
    
    @EventHandler
    public void onItemPickup(PlayerPickupItemEvent event) {
        Player player = event.getPlayer();
        PlayerData data = PlayerData.get(player);
        
        // Check for item duplication
        ItemStack item = event.getItem().getItemStack();
        if (inventoryChecks.checkItemDupe(player, data, item)) {
            alertManager.alert(player, "ItemDupe", "B");
            event.setCancelled(true);
        }
    }
    
    @EventHandler
    public void onItemDrop(PlayerDropItemEvent event) {
        Player player = event.getPlayer();
        PlayerData data = PlayerData.get(player);
        
        // Check for item duplication
        ItemStack item = event.getItemDrop().getItemStack();
        if (inventoryChecks.checkItemDupe(player, data, item)) {
            alertManager.alert(player, "ItemDupe", "C");
            event.setCancelled(true);
        }
    }
}